var myAtoi = function (s) {
  let sign = 1;
  let res = 0;
  let m = s.length;
  let i = 0;
  while (i < m && s[i] == " ") {
    i++;
  }
  let start = i;
  let max = Math.pow(2, 31) - 1;
  let min = -Math.pow(2, 31);
  for (; i < m; i++) {
    let c = s[i];
    if (i == start && c == "+") {
      sign = 1;
    } else if (i == start && c == "-") {
      sign = -1;
    } else if (c !== " " && Number.isInteger(Number(c))) {
      let num = c - "0";
      if (res > max / 10 || (res == max / 10 && num > max % 10)) {
        return max;
      }

      if (res < min / 10 || (res == min / 10 && -num < min % 10)) {
        return min;
      }
      res = res * 10 + sign * num;
    } else {
      break;
    }
  }
  return res;
};
console.log(myAtoi("   +0 123"));
console.log(myAtoi("4193 with words"));
